<?php

namespace App\Http\Controllers\Home;

use App\QiDian;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Models\AdminUser as User;
use App\Models\Domainame;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->Domainame = new Domainame();
    }

    public function index()
    {
        $data = 1123;
        var_dump($data);
    }

    public function checkQqSingle()
    {
        //程序运行时间
        $starttime = explode(' ', microtime());
        //echo microtime() ;
        /*········以下是代码区·········*/
        $sxCount = count(Domainame::whereIn('status', [1, 0])->where('ischeckqq', 0)->lists('id')) - 1;
        if ($sxCount == 1) {
            Domainame::where('ischeckqq', 1)->update(['ischeckqq' => 0]);
        }
        echo "还有【" . $sxCount ? $sxCount : 0 . "】个域名没有检测。";
        $lists = Domainame::checkQqSingle([1, 0], 1);
        $domain = "Null";
        if (count($lists)) {
            $ids = array();
            $domainArr = array();
            foreach ($lists as $keyList => $objList) {
                $ids[] = $objList->id;
                $domainArr[] = $objList->domain;
            }
            $res = CheckDomainInQQ($domainArr);
            foreach ($res as $keyCheck => $objCheck) {
                $domain = $objCheck['domain'];
                if ($objCheck['status'] == 0) {
                    $twoCheck = CheckDomainInQQ(array($objCheck['domain']));
                    $twoCheck[0]['content'] = e($twoCheck[0]['content']);
                    if ($twoCheck[0]['status'] == 0) {

                        foreach ($lists as $keyList => $objList) {
                            if ($objCheck['domain'] === $objList->domain) {
                                //SMS通知
                                $mobile = User::getMobile($objList->uid);
                                $data['mobile'] = config('app.debug') ? "13694236191" : $mobile;
                                $smsRes = "";
                                if (preg_match("/^1[34578]\d{9}$/", $mobile)) {
                                    $smsRes = sendSMS($data['mobile'], array($objList->domain, date('H'), date('i')), "165551");
                                }
                                if (strstr($smsRes, "success")) {
                                    //如果微信也屏蔽了，status => -2 反之 设置为-1
                                    if ($objList->status === 0) {
                                        Domainame::where('id', $objList->id)->update(['status' => -2, 'qq_check_res' => $twoCheck[0]['content']]);
                                    } else {
                                        Domainame::where('id', $objList->id)->update(['status' => -1, 'qq_check_res' => $twoCheck[0]['content']]);
                                    }
                                }
                                break;
                            }
                        }
                        $data['playMusic'] = true;
                    } else {
                        break;
                    }

                }
            }
            foreach ($lists as $keyId => $objId) {
                $ids[] = $objId['id'];
            }
            //检测域名
            if (!empty($ids)) Domainame::whereIn('id', $ids)->update(['ischeckqq' => 1]);
        }
        /*········以上是代码区·········*/
        echo "<br>";
        echo "当前QQ检测域名：" . $domain;
        //程序运行时间
        $endtime = explode(' ', microtime());
        $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
        $thistime = round($thistime, 3);
        echo "<br/>本网页执行耗时：" . $thistime . " 秒。" . time();
        echo "<br/>最新检测时间：" . date("Y/m/d H:i:s");
        header("Refresh: 1");
    }

    public function checkWechatSingle()
    {
        //程序运行时间
        $starttime = explode(' ', microtime());
        //echo microtime() ;
        /*········以下是代码区·········*/

        $tempId = "161985";
        //微信检测
        $data['lists'] = Domainame::whereIn('status', [1, -1])->orWhere('status', null)->where('ischeck', 0)->where('check_type', '<>', 0)
            ->select('id', 'uid', "domain", "status")
            ->orderBy('updated_atwx', 'asc')->limit(1)->get();
        if (count($data['lists']) === 0) {
            Domainame::where('ischeck', 1)->update(['ischeck' => 0]);
            $data['lists'] = $this->getDomainList();
        }
        $api = "http://vip.weixin139.com/weixin/wx_domain.php?user=1092506064&key=27ffb6b196ddcaa245bbcfc3ff05282f&domain=";

        if (count($data['lists']) !== 0) {
            foreach ($data['lists'] as $key => $obj) {
                //微信检测
                $res = $this->curlCkeck($api . $obj->domain);
                $data['res'] = $res;
                //判断是否被拦截
                if ($res['status'] == 2) {
                    //SMS通知
                    $mobile = User::getMobile($obj->uid);
                    $data['mobile'] = config('app.debug') ? "13694236191" : $mobile;
                    $smsRes = "";
                    if (preg_match("/^1[34578]\d{9}$/", $mobile)) {
                        $smsRes = sendSMS($data['mobile'], array($obj->domain, date('H'), date('i')), "165551");
                    }
                    if (strstr($smsRes, "success")) {
                        //如果微信也屏蔽了，status => -2 反之 设置为-1
                        if ($obj->status === 0) {
                            Domainame::where('id', $obj->id)->update(['status' => -2]);
                        } else {
                            Domainame::where('id', $obj->id)->update(['status' => -1]);
                        }
                    }
                    break;
                }
                Domainame::where('id', $obj->id)->update(['ischeck' => 1, 'updated_atwx' => date('Y-m-d H:i:s')]);
                $checkListWaitCount = Domainame::whereIn('status', [1, -1])->where('ischeck', 0)->where('check_type', '<>', 0)
                    ->select('id', 'uid', "domain", "status")
                    ->where('check_type', '<>', 0)->count();
                if ($checkListWaitCount === 0) {
                    Domainame::where('ischeck', 1)->update(['ischeck' => 0]);
                    $checkListWaitCount = Domainame::whereIn('status', [1, -1])->where('ischeck', 0)->where('check_type', '<>', 0)
                        ->select('id', 'uid', "domain", "status")
                        ->where('check_type', '<>', 0)->count();
                }

            }
        }

        echo "<br>";
        echo count($data['lists']);
        echo "<br>";
        /*········以上是代码区·········*/
        //程序运行时间
        $endtime = explode(' ', microtime());
        $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
        $thistime = round($thistime, 3);
        echo "<br/>本网页执行耗时：" . $thistime . " 秒。" . time();
        echo "<br/>最新检测时间：" . date("Y/m/d H:i:s");
        header("Refresh: 2");
    }

    public function curlCkeck($weixin_url)
    {
        $timeout = 3;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $weixin_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        $arr = json_decode($result, true);
        return $arr;
    }

    public function wechatAll()
    {
        //程序运行时间
        $starttime = explode(' ', microtime());
        /*········以下是代码区·········*/
        $lists = $this->Domainame->getDomainAll("wechat", [-1, 1]);

        if (count($lists)) {
            $ids = array();
            $domainArr = array();
            foreach ($lists as $keyList => $objList) {
                $ids[] = $objList->id;
                $domainArr[] = $objList->domain;
            }

            $resObj = checkDomainInWechat($domainArr);
            if (!empty($resObj->list)) {
                foreach ($resObj->list as $keyCheck => $objCheck) {
                    if ($objCheck->status === -1) {
                        foreach ($lists as $keyList => $objList) {
                            if ($objCheck->domain === $objList->domain) {
                                //SMS通知
                                $mobile = User::getMobile($objList->uid);
                                $data['mobile'] = config('app.debug') ? "13694236191" : $mobile;
                                $smsRes = "";
                                if (preg_match("/^1[34578]\d{9}$/", $mobile)) {
                                    $smsRes = sendSMS($data['mobile'], array($objList->domain, date('H'), date('i')), "165551");
                                }
                                if (strstr($smsRes, "success")) {
                                    //如果微信也屏蔽了，status => -2 反之 设置为-1
                                    if ($objList->status === 0) {
                                        Domainame::where('id', $objList->id)->update(['status' => -2]);
                                    } else {
                                        Domainame::where('id', $objList->id)->update(['status' => -1]);
                                    }
                                }
                                break;
                            }
                        }
                        $data['playMusic'] = true;
                    }
                    break;
                }
            }

            if (!empty($ids))
                Domainame::whereIn('id', $ids)->update(['ischeck' => 1, 'updated_atwx' => date('Y-m-d H:i:s')]);
        }

        echo "<br>";
        echo count($lists);
        echo "<br>";
        /*········以上是代码区·········*/
        //程序运行时间
        $endtime = explode(' ', microtime());
        $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
        $thistime = round($thistime, 3);
        echo "<br/>本网页执行耗时：" . $thistime . " 秒。" . time();
        echo "<br/>最新检测时间：" . date("Y/m/d H:i:s");
        header("Refresh: 600");
    }

    public function qqAll()
    {
        //程序运行时间
        $starttime = explode(' ', microtime());
        //echo microtime() ;
        /*········以下是代码区·········*/
        $lists = $this->Domainame->getDomainAll("QICQ", [0, 1]);
        if (count($lists)) {
            $ids = array();
            $domainArr = array();
            foreach ($lists as $keyList => $objList) {
                $ids[] = $objList->id;
                $domainArr[] = $objList->domain;
            }
            $res = CheckDomainInQQ($domainArr);
            foreach ($res as $keyCheck => $objCheck) {
                if ($objCheck['status'] == 0) {
                    $twoCheck = CheckDomainInQQ(array($objCheck['domain']));
                    $twoCheck[0]['content'] = e($twoCheck[0]['content']);
                    if ($twoCheck[0]['status'] == 0) {

                        foreach ($lists as $keyList => $objList) {
                            if ($objCheck['domain'] === $objList->domain) {
                                //SMS通知
                                $mobile = User::getMobile($objList->uid);
                                $data['mobile'] = config('app.debug') ? "13694236191" : $mobile;
                                $smsRes = "";
                                if (preg_match("/^1[34578]\d{9}$/", $mobile)) {
                                    $smsRes = sendSMS($data['mobile'], array($objList->domain, date('H'), date('i')), "165551");
                                }
                                if (strstr($smsRes, "success")) {
                                    //如果微信也屏蔽了，status => -2 反之 设置为-1
                                    if ($objList->status === 0) {
                                        Domainame::where('id', $objList->id)->update(['status' => -2, 'qq_check_res' => $twoCheck[0]['content']]);
                                    } else {
                                        Domainame::where('id', $objList->id)->update(['status' => -1, 'qq_check_res' => $twoCheck[0]['content']]);
                                    }
                                }
                                break;
                            }
                        }
                        $data['playMusic'] = true;
                    } else {
                        break;
                    }

                }
            }
            foreach ($lists as $keyId => $objId) {
                $ids[] = $objId['id'];
            }
            if (!empty($ids))
                Domainame::whereIn('id', $ids)->update(['updated_at' => date('Y-m-d H:i:s')]);
        }
        /*········以上是代码区·········*/
        echo "<br>";
        echo count($lists);
        echo "<br>";
        //程序运行时间
        $endtime = explode(' ', microtime());
        $thistime = $endtime[0] + $endtime[1] - ($starttime[0] + $starttime[1]);
        $thistime = round($thistime, 3);
        echo "<br/>本网页执行耗时：" . $thistime . " 秒。" . time();
        echo "<br/>最新检测时间：" . date("Y/m/d H:i:s");
        header("Refresh: 10");
    }

    public function testdata()
    {
        return response()->json(['return_code' => '3', 'return_message' => 'fuck!you', 'data' => Input::get(), 'get' => $_GET]);
    }
}
